﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSearchPaymentIn : UserControl, InterfaceSearchControl
    {
        private bool _isNotFill = true;
        private bool _openOnDblClick = false;
        private int _pid = -1;


        public UserControlSearchPaymentIn()
            :this(null)
        {
        }

        public UserControlSearchPaymentIn(int? pid)
        {
            InitializeComponent();
            if (pid.HasValue)
            {
                _pid = pid.Value;
            }
            radGridViewList.MultiSelect = true;
        }

        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewList.EnableFiltering;
            }
            set
            {
                radGridViewList.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                DataSetFSO.VIEW_SEARCH_PAYMENT_INRow dr;
                DataRow returnRow;
                if (radGridViewList.SelectedRows.Count > 0)
                {
                    dr = (DataSetFSO.VIEW_SEARCH_PAYMENT_INRow)(((DataRowView)(radGridViewList.SelectedRows.First()).DataBoundItem)).Row;
                    returnRow = paymenT_INTableAdapter1.GetDataByID(dr.ID).FindByID(dr.ID);
                }
                else
                {
                    returnRow = null;
                }
                return returnRow;
            }
        }

        public DataSet OwnerDataSet
        {
            get { return dataSetFSO1; }
            set { _isNotFill = false; dataSetFSO1 = (DataSetFSO)value; InitializeGridView(); }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewList.SelectedRows;
            }
        }

        public void RefreshList()
        {
            if (_pid == -1)
            {
                vieW_SEARCH_PAYMENT_INTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_PAYMENT_IN);
            }
            else
            {
                vieW_SEARCH_PAYMENT_INTableAdapter1.FillByPID(dataSetFSO1.VIEW_SEARCH_PAYMENT_IN, _pid);
            }
        }

        private void InitializeGridView()
        {
            GridViewTextBoxColumn colNumber = new GridViewTextBoxColumn("NUMBER", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.NUMBERColumn.ColumnName);
            colNumber.HeaderText = "№";

            GridViewDateTimeColumn colDate = new GridViewDateTimeColumn("DATE", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.DATEColumn.ColumnName);
            colDate.HeaderText = "Дата";
            colDate.FormatString = "{0:d}";

            GridViewTextBoxColumn colTypeOfAct = new GridViewTextBoxColumn("TYPE_OF_ACTIVITY", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.NSI_TYPE_OF_ACTIVITY_NAMEColumn.ColumnName);
            colTypeOfAct.HeaderText = "Направление";

            GridViewDecimalColumn colSum = new GridViewDecimalColumn("SUM", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.SUM_RUBColumn.ColumnName);
            colSum.HeaderText = "Сумма";
            colSum.Minimum = 0;
            colSum.FormatString = "{0:N2} Руб.";


            GridViewTextBoxColumn colOrg = new GridViewTextBoxColumn("ORGANIZATION", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.NSI_ORGANIZATION_NAMEColumn.ColumnName);
            colOrg.HeaderText = "Организация";

            GridViewTextBoxColumn colContract = new GridViewTextBoxColumn("CONTRACT", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.CONTRACT_NUMBERColumn.ColumnName);
            colContract.HeaderText = "Договор";


            GridViewTextBoxColumn colGroups = new GridViewTextBoxColumn("PROD_GROUPS", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.PROD_GROUPSColumn.ColumnName);
            colGroups.HeaderText = "Группы товаров";

			GridViewDecimalColumn colPayInItemSum = new GridViewDecimalColumn("PAY_IN_ITEM_SUM", dataSetFSO1.VIEW_SEARCH_PAYMENT_IN.PAYMENT_IN_ITEM_SUMColumn.ColumnName);
			colPayInItemSum.HeaderText = "Сумма платежа по товару";
			colPayInItemSum.Minimum = 0;
			colPayInItemSum.FormatString = "{0:N2} Руб.";

            radGridViewList.MasterTemplate.Columns.Clear();
            radGridViewList.MasterTemplate.Columns.Add(colNumber);
            radGridViewList.MasterTemplate.Columns.Add(colDate);
            radGridViewList.MasterTemplate.Columns.Add(colSum);           
            radGridViewList.MasterTemplate.Columns.Add(colOrg);
            radGridViewList.MasterTemplate.Columns.Add(colContract);
            radGridViewList.MasterTemplate.Columns.Add(colTypeOfAct);
            radGridViewList.MasterTemplate.Columns.Add(colGroups);
			radGridViewList.MasterTemplate.Columns.Add(colPayInItemSum);
			

            radGridViewList.DataSource = dataSetFSO1.VIEW_SEARCH_PAYMENT_IN;
        }

        private void radGridViewList_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            DataSetFSO.PAYMENT_INRow selectedRow = (DataSetFSO.PAYMENT_INRow)this.SelectedRow;
            if (selectedRow != null && _openOnDblClick)
            {
                FormPaymentIn formPayIn = new FormPaymentIn(selectedRow);
                if (!FormHelper.HasOpenedForm(formPayIn.FormName))
                {
                    formPayIn.MdiParent = FormHelper.MainForm;
                    formPayIn.Show();
                    formPayIn.Select();
                }
            }
        }

        private void UserControlSearchPaymentIn_Load(object sender, EventArgs e)
        {
            if (_isNotFill)
            {
                RefreshList();
            }
            InitializeGridView();
        }

        public void FillPayOnService()
        {
            vieW_SEARCH_PAYMENT_INTableAdapter1.FillByPidIsNull(dataSetFSO1.VIEW_SEARCH_PAYMENT_IN);
            _isNotFill = false;
        }
    }
}
